﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BulldogCMS.Data.FilesTree
{
    public class XMLPartialFileNode : IPartialFileNode
    {
        private XElement _element = null;
        private List<IPartialFileNode> _subPartialNodes = null;

        public XMLPartialFileNode(XElement element)
        {
            this._element = element;
            this.ID = this._element.GetAttributeValue<Guid>("ID");
            this.Name = this._element.GetAttributeValue<string>("name");
        }

        public IEnumerable<IPartialFileNode> SubPartialNodes
        {
            get
            {
                if (this._subPartialNodes == null)
                {
                    this._subPartialNodes = this.LoadPartialNodes();
                }
                return this._subPartialNodes;
            }
        }

        private List<IPartialFileNode> LoadPartialNodes()
        {
            List<IPartialFileNode> partialNodes = new List<IPartialFileNode>();
            foreach (var nodeElement in this._element.Elements("node"))
            {
                XMLPartialFileNode partialNode = new XMLPartialFileNode(nodeElement);
                partialNodes.Add(partialNode);
            }
            return partialNodes;
        }

        public string Name
        {
            get;
            internal set;
        }

        public Guid ID
        {
            get;
            internal set;
        }

        public string TemplateName
        {
            get {
                return "Folder";
            }
        }



    }
}
